/*******************************************************************************
 * Copyright 2011 Alexandre Zglav and Morphiastic
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.morphiastic.analysis.tokenizers;

import java.io.IOException;
import java.util.Map;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import com.morphiastic.analysis.AnalysisComponentDefinition;
import com.morphiastic.utils.EqualsUtil;
public class StandardTokenizer extends ElasticTokenizer {

	public static final String MAX_TOKEN_LENGTH_PARAM_KEY = "max_token_length";
	private int maxTokenLength = -1;
	
	
	public StandardTokenizer(String defName) {
		this(null, defName);
	}

	public StandardTokenizer(Map<String,String> params, String defName) {
		super(ElasticTokenizerType.standard, defName);
		
		if(params!=null){
			String marTokLStr = params.get(MAX_TOKEN_LENGTH_PARAM_KEY);
			if(marTokLStr != null && !marTokLStr.isEmpty()){
				try {
					int maxTokL = Integer.parseInt(marTokLStr);
					this.maxTokenLength = maxTokL;
				} catch (NumberFormatException e) {
				}
			}
		}
	}
	

	
	@Override
	public boolean hasCustomParams() {
		 if(this.maxTokenLength!=-1 )return true;
		 return false;
	}

	@Override
	public XContentBuilder addToBuilder(XContentBuilder mappingBuilder) throws IOException {
		
		mappingBuilder.startObject(getDefName())
		.field("type", getElasticTokenizerType().toString());
		
		//add custom params
		if(this.maxTokenLength>0){
			mappingBuilder.field(MAX_TOKEN_LENGTH_PARAM_KEY, this.maxTokenLength);
		}
		return mappingBuilder;
	}

	@Override
	public void setParamsFromSettings(Settings tokSettings) {
		
		
		String maxTokenLengthSetting = tokSettings.get(MAX_TOKEN_LENGTH_PARAM_KEY);
		if(maxTokenLengthSetting!=null && !maxTokenLengthSetting.isEmpty()){
			try {
				int mtl = Integer.parseInt(maxTokenLengthSetting);
				this.maxTokenLength = mtl;
			} catch (NumberFormatException e) {
			}
		}
	}

	@Override
	public boolean areEquivalentDefinitions(AnalysisComponentDefinition other){
		
		if(other==null)return false;
		StandardTokenizer ot ;
		try {
			ot = (StandardTokenizer)other;
		} catch (ClassCastException e) {
			return false;
		}
		return 
		EqualsUtil.areEqual(this.maxTokenLength, ot.maxTokenLength);
	}
		
	
}
